define(['cookie'], function(cookie) {
    return {
        init: function() {

            //console.log(location.search);//?sid=4
            let sid1 = location.search.split('=')[1];
            /*    console.log(sid1 + "1"); */
            $.ajax({
                    type: "get",
                    url: `http://localhost/wmsj/php/getsid.php`,
                    data: {
                        sid: sid1
                    },
                    dataType: "json"
                }).done((data) => {

                    //console.log(data);
                    $('.tempWrap').find('ul').css('left', 0);
                    $('#bf').find('img').attr('src', data.url);

                    let strhtml = `<ul>`;
                    let $tempurl = `<ul style="width: 1720px; left: 0px; position: relative; overflow: hidden; padding: 0px; margin: 0px;">`;
                    let $picurl = JSON.parse(data.piclisturl);
                    // console.log($picurl);
                    // console.log(typeof data.piclisturl);  //string
                    $.each($picurl, function(index, value) {
                        //    console.log(1);
                        strhtml += `
                
                            <li ><img src="${value.src}" width="100" height="100" alt="" ></li>
                           `;

                        $tempurl += `
                        <li class='1q' style="float: left; width: 430px;"><img src="${value.src}" width="430" height="430"></li>


`;



                    })
                    strhtml += `</ul>`;
                    $tempurl += `</ul>`;

                    $('.hd').append(strhtml);
                    $('.tempWrap').append($tempurl);




                })
                /* 
                            console.log(this); */

            /* ******lizhi******* */
<<<<<<< HEAD
            if (cookie.get('users')) {
                $(function() {
                    // console.log(1);
                    let usersname = cookie.get('users');
                    // console.log(usersname);
                    $('.user-reg').css({ 'display': 'none' });
                    $('.usersdata').css({ 'display': 'block' });
                    $('.usersdata').text(usersname);
                    /*   console.log($('.checkusers').eq(1).find('.names').text(usersname)); */
                });
            }
=======
            $(document).ready(function() {
                if (cookie.get('users')) {

                    let usersname = '';
                    $(function() {

                        usersname = cookie.get('users');
                        // console.log(usersname);
                        $('.user-reg').css({ 'display': 'none' });
                        $('.usersdata').css({ 'display': 'block' });
                        $('.usersdata').text(usersname);

                    });

                }
            });







>>>>>>> last commit


            /* ****** */

            this.fanda();
            this.jisuan();
            this.shopcar();

        },
        fanda: function() {

            let $sf = $('#sf');
            let $wrap = $('.tempWrap');
            $('.tempWrap').on('mousemove', (ev) => {
                /*   console.log(ev.offsetX); */

                $('#bf').css('visibility', 'visible');





                let $left = ev.offsetX - $sf.width() / 2; //outer  
                //pointer-events: none 解决了offsetX的问题
                let $top = ev.offsetY - $sf.height() / 2;
                if ($left <= 0) {
                    $left = 0;
                }
                if ($left > (430 - $sf.width())) {
                    $left = 430 - $sf.width();
                }

                if ($top <= 0) {
                    $top = 0;
                }
                if ($top > (430 - $sf.height())) {
                    $top = 430 - $sf.height();
                }
                /*    let $left = ev.pageX - $wrap.offset().left - $sf.width() / 2;
                   let $top = ev.pageY - $wrap.offset().top - $sf.height() / 2; */


                /*  $sf.offset().left = left;
                 $sf.offset().top = top; */


                /*   console.log(left); */

                $sf.css({ top: $top, left: $left }); //会闪烁

                /* 
                                $sf.style.left = left;
                                $sf.style.top = top; */


                /* *********放大功能********** */

                let $yuantubi = $('#sf').width() / $('.tempWrap').find('li:first').width();
                //  console.log($yuantubi);
                let $fandatubi = $('#bf').width() / $('#bf').find('img').width();
                // console.log($fandatubi);
                let $fanleftx = $yuantubi / $fandatubi;

                let $yuantubiy = $('#sf').height() / $('.tempWrap').find('li:first').height();
                //  console.log($yuantubi);
                let $fandatubiy = $('#bf').height() / $('#bf').find('img').height();
                // console.log($fandatubi);
                let $fantop = $yuantubiy / $fandatubiy;



                let $yuanx = $('.tempWrap').find('li:first').width(); //$('#sf').width() /
                //  console.log($yuantubix);0.43
                let $fandax = $('#bf').find('img').width(); //$('#bf').width() /
                // console.log($fandatubix);0.5
                let $fanleft = $left / $yuanx * $fandax * $fanleftx;



                let $yuany = $('.tempWrap').find('li:first').height(); //$('#sf').height() /
                //  console.log($yuantubiy);0.43
                let $fanday = $('#bf').find('img').height(); //$('#bf').width() /
                // console.log($fandatubiy);0.5
                let $fanheight = $top / $yuany * $fanday * $fantop;


                $('#bf').find('img').css({
                    left: -$fanleft,
                    top: -$fanheight
                });









            });

            $('.tempWrap').on('mouseout', (ev) => {
                $('#bf').css('visibility', 'hidden');


            });
            $('.hd').on('click', 'li', function() {
                //let xsrc = $(this).find('img')[0].src;
                $(this).addClass('on').siblings().removeClass('on');
                let xsrc = $(this).find('img').attr('src');
                //console.log(xsrc);
                $('#bf').find('img').attr('src', xsrc);

                let $width = $('.tempWrap').find('li:first').width();
                //console.log($width);
                // console.log(this);
                // console.log($('.tempWrap').find('li'));
                // console.log($('.hd').find('li').index($(this)));
                // console.log($('.tempWrap').find('ul') == $(this).parent());
                ;
                let $indexnum = $(this).index();
                $indexnum;
                $('.tempWrap').find('ul').css('left', -$width * $indexnum);


            })










        },

        jisuan: function() {
            let $plus = $('.plus');
            let $reduce = $('.reduce');
            $plus.on('click', function() {

                let $val = $(this).siblings('#buyNumber').val();
                $val++;
                $(this).siblings('#buyNumber').val($val);

                // console.log($(this).siblings('#buyNumber').val());



            });
            $reduce.on('click', function() {

                let $val = $(this).siblings('#buyNumber').val();
                $val--;
                $(this).siblings('#buyNumber').val($val);

                //console.log($(this).siblings('#buyNumber').val());



            });

        },
        shopcar: function() {
            let arr = [];
            if (cookie.get('cookies')) {


                //  let arr1 = cookie.get('cookies');
                //console.log(arr1);
                arr.push(cookie.get('cookies'));





                console.log(arr);
            }

            let $addCart = $('.btn_addCart');
            $addCart.on('click', function() {
                let cookiesid = location.search.split('=')[1];
                let cookienum = $('#buyNumber').val();
                let cookielist = `${cookiesid}:${cookienum}`;
                let key = [];

                //     console.log(cookielist);
                // console.log(arr.length == 0);
                if (!arr.length == 0) {
                    //console.log(arr);
                    //  console.log(1);
                    let flag = 0;
                    for (let i = 0; i < arr.length; i++) {
                        //console.log(1); 1 5
                        let key = parseInt(arr[i].split(':')[0]);
                        let keynum = parseInt(arr[i].split(':')[1]);
                        //console.log(arr[i]);
                        //  console.log(key); //5=1;
                        //console.log(keynum);
                        // console.log(key == cookiesid); //key是数组
                        if (key == cookiesid) {
                            // console.log(11);
                            //console.log(cookienum);
                            keynum += +cookienum;
                            // console.log(keynum);
                            arr[i] = `${cookiesid}:${keynum}`;
                            flag = 1;
                        } else {}
                    }
                    if (flag == 0) {
                        arr.push(cookielist);
                    }

                    console.log(arr);
                    cookie.set('cookies', arr, 124);
                } else {
                    arr.push(cookielist);
                    cookie.set('cookies', arr, 124);
                    //  console.log(arr);
                }



                if (cookie.get('users') && cookie.get('cookies')) {

                    let usersname = '';
                    usersname = cookie.get('users');
                    let datas = cookie.get('cookies');
                    console.log(usersname, datas);
                    $.ajax({
                        type: 'post',
                        url: 'http://localhost/wmsj/php/reg1.php',
                        dataType: 'json',
                        data: {
                            username: usersname,
                            password: datas + '-aaa'
                        }

                    }).done(function(data) {
                        // console.log(1);
                        console.log(data);
                    })
                    console.log(1);







                }






            })




        }



    }



})